<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AxGridView
 *
 * @author ardha
 * @created on Oct 24, 2012
 */
Yii::import('zii.widgets.grid.CGridView');
Yii::import("ext.widgets.grid.AxLinkListPager");
Yii::import("ext.widgets.grid.AxPageSize");

class AxGridView extends CGridView {

    public $createButtonUrl = '';
    public $createButtonTitle = 'Create New';
    public $createButtonType = 'normal';
    public $createButtonProperty = array('htmlOptions' => array('class' => 'ax-btn', 'type' => 'button'));
    public $createTopButton = true;
    public $createBottomButton = false;
    public $deleteTopButton = true;
    public $deleteBottomButton = false;
    public $deleteButtonUrl = 'delete';
    public $deleteConfirmation = 'Are you sure to delete the Data?';
    public $deleteButtonProperty = array('label' => 'Delete', 'cssClass' => 'ma-action', 'url' => '#', 'htmlOptions' => array('class' => 'ax-btn-delete', 'type' => 'button'));
    public $cssButtonClass = 'btn-right';
    public $enablePageSize = true;
    public $pageSize = 10;
    public $title;
    public $checkColumn = array('class' => "CCheckBoxColumn");
    public $checkBoxClmId = 'chckbx-clmn';
    public function init() {
        
        if($this->createButtonUrl=="")
            $this->createButtonUrl = Yii::app()->createUrl($this->controller->id.'/create');
        
            
        $this->cssFile = false;
        //$this->summaryText = false;
        if($this->deleteTopButton)
            $this->setCheckBoxColumn();
        $this->pager = array(
            //'class' => 'CLinkPager',
            'class' => 'AxLinkListPager',
            'maxButtonCount' => '8'
        );
        $this->pagerCssClass = "pagination";
        parent::init();
        
    }
    public function renderContent() {
        $this->renderTitleBar();
        echo '<div class="ax-body-panel">';
        parent::renderContent();
        echo '</div>';
        
        
    }
    public function renderSummary() {
        parent::renderSummary();
        if($this->createTopButton || $this->deleteTopButton){
            echo CHtml::openTag('div', array('class' => $this->cssButtonClass));
            if ($this->createTopButton)
                $this->renderCreateButton();
            if($this->deleteTopButton){
                //
                //$this->columns = array_merge(array('class' => 'CCheckBoxColumn'), $this->columns);
                echo '&nbsp;';
                $this->renderDeleteButton();
            }
            
            echo CHtml::closeTag('div');
            echo '<div style="clear: both;"></div>';
        }
    }
    public function renderPager() {
        /*if($this->createBottomButton || $this->deleteBottomButton){
            echo CHtml::openTag('div', array('class' => $this->cssButtonClass, 'style' => 'margin-top:2px;'));
            if ($this->createBottomButton)
                $this->renderCreateButton();
            echo CHtml::closeTag('div');
        }*/
        parent::renderPager();
    }
    public function renderItems() {
        //$this->renderPageSize();
        
        
        parent::renderItems();
    }
    public function setCheckBoxColumn() {
       $this->checkColumn['htmlOptions'] = array('align' => 'center', 'valign' => 'top');
       $this->checkColumn['headerHtmlOptions'] = array('style' => 'text-align:center');
       if (!isset($this->checkColumn['selectableRows']))
            $this->checkColumn['selectableRows'] = 2;
        if (!isset($this->checkColumn["id"]))
            $this->checkColumn["id"] = $this->checkBoxClmId;
       
        
        $this->columns = array_merge(array($this->checkColumn), $this->columns);
    }
    private function renderCreateButton() {
        if ($this->createButtonType == 'long')
            $this->createButtonProperty['htmlOptions']['class'] = 'ax-btn';
        $this->createButtonProperty['htmlOptions']['onclick'] = 'MM_goToURL("' . $this->createButtonUrl . '")';
        echo CHtml::button($this->createButtonTitle, $this->createButtonProperty['htmlOptions']);
        
    }
    private function renderDeleteButton() {
        $this->deleteButtonProperty['htmlOptions']['onclick'] = 'deleteAction()';
        echo CHtml::button($this->deleteButtonProperty['label'], $this->deleteButtonProperty['htmlOptions']);
    }
    public function renderPageSize() {
        if (!$this->enablePageSize)
            return;

        $pageSize = array();
        $class = 'AxPageSize';
        if (is_string($this->pageSize))
            $class = $this->pageSize;
        else if (is_array($this->pageSize)) {
            $pageSize = $this->pageSize;

            if (isset($pageSize['class'])) {
                $class = $pageSize['class'];
                unset($pageSize['class']);
            }
        }
        $pageSize["afterUpdate"] = "
                $.fn.yiiGridView.update('{$this->id}');
            ";
        $pageSize['pages'] = $this->dataProvider->getPagination();
        echo CHtml::openTag("div", array("class" => "ma-actions"));
        $pageSize = $this->widget($class, $pageSize);
        echo CHtml::closeTag("div");
        //$this->renderPager();
    }
    public function renderTitleBar(){
        echo '<div class="x-panel-header-default x-horizontal x-panel-header-horizontal x-panel-header-default-horizontal x-top x-panel-header-top x-panel-header-default-top x-docked-top x-panel-header-docked-top x-panel-header-default-docked-top x-unselectable" style="height:17px;border:0px;">';
        echo '                    <span class="x-panel-header-text x-panel-header-text-default">';
        echo '                        '.$this->title;
        echo '                    </span>';
        echo '</div>';
    }
    public function registerClientScript() {
        parent::registerClientScript();
        $cs = Yii::app()->getClientScript();
        $js = <<<EOS
function MM_goToURL(ulink)
{
    window.location=ulink;
}\n     
EOS;

        
        $cs->registerScript(__CLASS__ . '#' . $this->id, $js, CClientScript::POS_END);
    }
    
}
?>